[PHP] access array element by value

Posted by Brandon on Stack Overflow See other posts from Stack Overflow or by Brandon
Published on 2010-06-17T17:06:27Z Indexed on 2010/06/17 17:13 UTC
Read the original article Hit count: 352

Filed under:
   array(
  [0]
      name => 'joe'
      size => 'large'
  [1] 
      name => 'bill'
      size => 'small'

)

I think i'm being thick, but to get the attributes of an array element if I know the value of one of the keys, I'm first looping through the elements to find the right one.

foreach($array as $item){
   if ($item['name'] == 'joe'){
      #operations on $item
   }
}

I'm aware that this is probably very poor, but I am fairly new and am looking for a way to access this element directly by value. Or do I need the key?

Thanks, Brandon

© Stack Overflow or respective owner

Related posts about php